import {defineComponent, onMounted, reactive, ref, toRefs} from "vue";
import type {FormInstance} from 'element-plus'
#set($lentityName="${entityName.substring(0, 1).toLowerCase()}${entityName.substring(1)}")

import ${lentityName}Api from "./${entityName}Api";

export default defineComponent({
    name: '${entityName}Update',
    props: ["id", "runMode"],
    setup: (props, ctx) => {
        const formInstance = ref<FormInstance>()
        const {id, runMode} = toRefs(props);
        console.log("传递进来的数据:{}", runMode);
        //表单数据
        const formData = ref({
        #foreach($col in $fullColumnList)
              //${col.column_comment}
              ${col.propertyName}: null,
        #end

        })
        //表单校验
        const formRules = reactive({
           #foreach($col in $fullColumnList)
        ${col.propertyName}: [
                    {required: true, message: '请输入${col.column_comment}', trigger: 'blur'}
                ],
            #end
        })
        const init = async () => {
            if (typeof id.value === 'undefined') {
                return;
            }
            let msg = await ${lentityName}Api.select${entityName}ById(id.value);
            formData.value = msg.data as any;
            console.log(formData.value);

        }
        const checkFormData = async () => {
            return new Promise((resolve, reject) => {
                // @ts-ignore
                formInstance.value.validate((valid, fixeds) => {
                    resolve(valid)
                })
            })
        }
        const doSubmit = async () => {
            console.log("保存")
            if (!formInstance)
                return;
            let result = await checkFormData();
            if (!result) {
                return;
            }
            return ${lentityName}Api.doSave${entityName}(formData.value);

        }
        const doUpdate =async () => {
            if (!formInstance)
                return;
            let result = await checkFormData();
            if (!result) {
                return;
            }
            return ${lentityName}Api.doUpdate${entityName}(formData.value);
        }
        onMounted(() => {
            init().then(r => {
            });
        })
        return {formInstance, formData, formRules, doSubmit, doUpdate}
    }
})